package com.handong.handserialprogram.module.catchcriminal;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.View;
import android.view.ViewStub;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
//import com.baidu.mapapi.map.BaiduMap;
//import com.amap.api.maps2d.MapView;
//import com.baidu.mapapi.map.Marker;
//import com.baidu.mapapi.model.LatLng;
import com.amap.api.maps.AMap;
import com.amap.api.maps.MapView;
import com.amap.api.maps.UiSettings;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Marker;
import com.handong.handserialprogram.R;
import com.handong.handserialprogram.constants.KeyConstant;
import com.handong.handserialprogram.module.base.BaseView;
import com.handong.handserialprogram.module.base.EmergencyActivity;
import com.handong.handserialprogram.utils.AMapUtil;

/**
 * 追逃模式界面
 */
public class CatchCriminalActivity extends EmergencyActivity<CatchCriminalPresenter> implements BaseView {

    private String mCriminalName, mRingID;
    private MapView mAView;
    private AMap mAMap;
    private TextView mTipTextView;
    private FrameLayout mPoliceLocate ,mCriminaLoacate;

    private View mTipLayoutBottom;
    private ImageView mTipLayoutPhoto;
    private TextView mTipLayoutCancel, mTipLayoutTitle, mTipLayoutDescription;
    private UiSettings uiSettings;
    protected String PoliceMarker="POLICE_MARKER";
    protected String CriminalMarker="CRIMINAL_MARKER";

    public AMap getmAMap() {
        return mAMap;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mAView.onCreate(savedInstanceState);
    }

    @Override
    protected int getContentViewID() {
        return R.layout.activity_catch_criminal;
    }

    @Override
    protected CatchCriminalPresenter createPresenter() {
        return new CatchCriminalPresenter(this);
    }

    @Override
    protected void initViewByID() {
        initHeaderLayout(false);
        //setOptionItem("驾车");
        mTipTextView = (TextView) findViewById(R.id.catchLocateTip);
        mAView = (MapView) findViewById(R.id.catchCriminalMap);
        mPoliceLocate = (FrameLayout) findViewById(R.id.mapPoliceLocate);
        mCriminaLoacate = (FrameLayout) findViewById(R.id.criminalLocate);
        mPoliceLocate.setOnClickListener(this);
        mCriminaLoacate.setOnClickListener(this);
        mAMap = mAView.getMap();
        uiSettings=mAMap.getUiSettings();
        uiSettings.setScaleControlsEnabled(true);
        uiSettings.setZoomControlsEnabled(false);
//        mAView.showZoomControls(false);//放大
//        mAView.showScaleControl(true);//比例尺

        mTipLayoutBottom = findViewById(R.id.viewStubCatchLayout);

        /*cstv= (TextView) findViewById(R.id.cstv);
        cstv1= (TextView) findViewById(R.id.cstv1);
        cstv2= (TextView) findViewById(R.id.cstv2);*/

    }

    /*StringBuffer stringBuffer=new StringBuffer();
    public void setCstvData(String date,String wz){
        stringBuffer.delete(0,stringBuffer.length());
        stringBuffer.append("服务器位置");
        if(!TextUtils.isEmpty(date)){
            stringBuffer.append(date);
        }
        if(!TextUtils.isEmpty(wz)){
            stringBuffer.append(wz);
        }
        cstv.setText(stringBuffer.toString());
    }

    StringBuffer stringBuffer1=new StringBuffer();
    public void setCstvData1(String date,String wz){
        stringBuffer1.delete(0,stringBuffer1.length());
        stringBuffer1.append("警察位置");
        if(!TextUtils.isEmpty(date)){
            stringBuffer1.append(date);
        }
        if(!TextUtils.isEmpty(wz)){
            stringBuffer1.append(wz);
        }
        cstv1.setText(stringBuffer1.toString());
    }

    StringBuffer stringBuffer2=new StringBuffer();
    public void setCstvData2(String date,String wz){
        stringBuffer2.delete(0,stringBuffer1.length());
        stringBuffer2.append("犯人位置");
        if(!TextUtils.isEmpty(date)){
            stringBuffer2.append(date);
        }
        if(!TextUtils.isEmpty(wz)){
            stringBuffer2.append(wz);
        }
        cstv2.setText(stringBuffer2.toString());
    }*/

    @Override
    protected void initViewDataRes() {
        //showLoadDialog("正在获取当前位置信息...");
        Bundle bundle = getIntent().getExtras();
        if (!bundle.isEmpty()) {
            mRingID = bundle.getString(KeyConstant.PERSON_RING_ID);
            mCriminalName = bundle.getString(KeyConstant.PERSON_NAME);
            mPresenter.backupRingID(mRingID);
            boolean isEscape = bundle.getBoolean(KeyConstant.PERSON_STATUS);
            if (isEscape) {
                refreshTipMsg(getString(R.string.tip_content_criminal_escape));
            } else {
                refreshTipMsg(getString(R.string.tip_content_criminal_normal));
            }
        }
        setCenterTitle("犯人 - " + mCriminalName);
        mPresenter.initMapModule(mRingID);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.headerBackLayout:
                finish();
                break;
            case R.id.headerOptionLayout:
                break;
            case R.id.mapPoliceLocate:
                mPresenter.locateToCenter(mAMap);
                break;
            case R.id.criminalLocate:
                mPresenter.locateToCenter1(mAMap);
            case R.id.catchTipLayoutCancel:
                hideBottomLayout();
                break;
            default:
                super.onClick(v);
        }
    }

    public void addPoliceOnMap(LatLng latLng, int resId) {
        Marker marker = AMapUtil.addMarkerOnMap(mAMap, latLng, resId);
        mPresenter.putMarker(PoliceMarker, marker);
        AMapUtil.locateToCenter(mAMap, latLng);
    }
    public void addCriminalOnMap(LatLng latLng, int resId) {
        Marker marker = AMapUtil.addMarkerOnMap(mAMap, latLng, resId);
        mPresenter.putMarker(CriminalMarker, marker);
        AMapUtil.locateToCenter(mAMap, latLng);
    }

    protected void showBottomLayout(boolean isCriminal) {
        View inflateView = null;
        if (mTipLayoutBottom instanceof ViewStub) {
            inflateView = ((ViewStub) mTipLayoutBottom).inflate();
            inflateView.measure(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

            mTipLayoutTitle = (TextView) inflateView.findViewById(R.id.catchTipLayoutTitle);
            mTipLayoutPhoto = (ImageView) inflateView.findViewById(R.id.catchTipPersonPhoto);
            mTipLayoutDescription = (TextView) inflateView.findViewById(R.id.catchTipPersonLocate);
            mTipLayoutCancel = (TextView) inflateView.findViewById(R.id.catchTipLayoutCancel);
            mTipLayoutCancel.setOnClickListener(this);
        }
        if (isCriminal) {
            mTipLayoutTitle.setText(R.string.bottom_tip_title_criminal);
            mTipLayoutPhoto.setImageResource(R.drawable.icon_criminal_photo);
        } else {
            mTipLayoutTitle.setText(R.string.bottom_tip_title_police);
            mTipLayoutPhoto.setImageResource(R.drawable.icon_police_photo);
            mTipLayoutDescription.setText(R.string.bottom_tip_locating);
        }
        if (mTipLayoutBottom instanceof ViewStub || View.VISIBLE != mTipLayoutBottom.getVisibility()) {
            if (inflateView != null) mTipLayoutBottom = inflateView;
            Animation animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.slide_bottom_in);
            animation.setDuration(150);
            mTipLayoutBottom.setAnimation(animation);
            mTipLayoutBottom.setVisibility(View.VISIBLE);
            //mAMap.setMapStatus(MapStatusUpdateFactory.scrollBy(0, 200));
            int distance = -(inflateView != null ? inflateView.getMeasuredHeight():mTipLayoutBottom.getHeight());
            moveMapActionView(mAView.getChildAt(1), distance);
            moveMapActionView(mAView.getChildAt(3), distance);
            moveMapActionView(mPoliceLocate, distance);
        }
    }

    private static void moveMapActionView(View view, float distance) {
        if (view!=null)
        view.animate().alpha(0.9f).translationY(distance).setDuration(150);
    }

    protected void hideBottomLayout() {
        if (View.VISIBLE != mTipLayoutBottom.getVisibility()) {
            return;
        }
        mPresenter.resetBottomStatus();
        //mAMap.setMapStatus(MapStatusUpdateFactory.scrollBy(0, -200));
        Animation animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.slide_bottom_out);
        animation.setDuration(150);
        mTipLayoutBottom.setAnimation(animation);
        mTipLayoutBottom.setVisibility(View.GONE);
        mPoliceLocate.animate().alpha(0.9f).translationY(0f).setDuration(150);

        moveMapActionView(mAView.getChildAt(1), 0f);
        moveMapActionView(mAView.getChildAt(3), 0f);
        moveMapActionView(mPoliceLocate, 0f);
    }

    protected void refreshLocateDescription(String address) {
        if (View.VISIBLE != mTipLayoutBottom.getVisibility()) {
            return;
        }
        mTipLayoutDescription.setText(address);
    }

    protected void refreshTipMsg(String tip) {
        mTipTextView.setText(tip);
    }

    protected void refreshTipMsg(int tip) {
        mTipTextView.setText(tip);
    }

    public AMap getAMap() {
        return mAMap;
    }

    @Override
    public void onBackPressed() {
        if (mTipLayoutBottom.isShown()) {
            hideBottomLayout();
            return;
        }
        super.onBackPressed();
    }

    @Override
    public void finish() {
        if (!mPresenter.isCanExit()) {
            showExitTip();
            return;
        }
        super.finish();
        overridePendingTransition(R.anim.anim_no, R.anim.push_right_out);
    }

    private void showExitTip() {
        MaterialDialog dialog = new MaterialDialog.Builder(this)
                .content("正在追踪逃脱犯人，是否退出？")
                .positiveText(R.string.dialog_positive_exit)
                .positiveColorRes(R.color.back_ground_green)
                .negativeText(R.string.dialog_negative_text)
                .negativeColorRes(R.color.bg_gray)
                .onPositive(new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                        mPresenter.forceExit();
                    }
                })
                .build();
        dialog.show();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mAView.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mAView.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mAMap = null;
        if (mAView != null) {
            mAView.onDestroy();
            mAView = null;
        }
    }
}
